$(function(){
    berserker.addView({
        name:'ylnc',
        tpl:'tpl_ylnc',
        dataURL:'/api/ylnc.json',
        coverRight:true,
        done:function(host,data){
            host.html(data.html);
            bindNav();
            function bindNav(){
                var pIndex = 0,
                pageSize = 6,
                pItemCount = $('.top-nav-box li',host).size(),
                pWidth = $('.top-nav-box li:first',host).width(),
                pageCount = Math.ceil( pItemCount / pageSize ) ,
                domCenter = $('#frm_center');

                $('.top-nav-box li',host).each(function(index,item){
                    if($(item).hasClass('top-nav-act')){
                        pIndex = Math.floor(index/pageSize);
                        $('.top-nav-box').css('margin-left',-pIndex * pageSize * pWidth+'px');
                        return false;
                    }
                })

                $('.top-nav-box',host).css('width',pWidth * pItemCount);
                $('.top-nav-box li',host).click(function(evt){
                    var that = this;
                    if($(that).hasClass('top-nav-act')){return;}
                    if(domCenter.data('loader')){
                        domCenter.data('loader').abort();
                    }
                    if(domCenter.data('loader')){
                        domCenter.data('loader').abort();
                    }
                    domCenter.data('loader',(function(evt){
                        return berserker.getData(that.getAttribute('data-url'),function(res){
                            domCenter.html(res)
                            domCenter.data('loader',null);
                            bindNav();
                        })
                    })())
                })
                /******************上一页,下一页*******************/
                $('.nav-pre',host).click(function(evt){
                    if(pIndex == 0)return;
                    //上一页
                    pIndex--;
                    $('.top-nav-box').css('margin-left',-pIndex * pageSize * pWidth+'px');
                })
                $('.nav-next',host).click(function(evt){
                    //下一页
                    if(pIndex>=pageCount-1)return;
                    pIndex++;
                    $('.top-nav-box',host).css('margin-left',-pIndex * pageSize * pWidth+'px');
                })
                /******************上一页,下一页 END *******************/
            }

        }
    })
})


